1 import random
2 import sqlite3
3
4
5 def check_luhn_algo(checkme):
6     converting_to_int_list = []
7     
for item in list(checkme):
8         converting_to_int_list.append(
int(item))
9     luhn_card_no = converting_to_int_list[:-
1]
10     tmp_list = luhn_card_no.copy()
11     
for i in range(0, len(tmp_list), 2):
12         tmp_list[i] *=
2
13         
if tmp_list[i] > 9:
14             tmp_list[i] -=
9
15     checksum = list(str(
10 - sum(tmp_list) % 10))
16     
if len(checksum) != 1:
17         checksum = [
0]
18     luhn_card_no.extend(checksum)
19     del tmp_list
20     card_no_for_db =
''.join(map(str, luhn_card_no))
21     
if card_no_for_db == checkme:
22         
return True
23     
else:
24         
return False
25
26
27 def create_card():
28     def get_pin():
29         pin =
""
30         
for each in random.sample(range(9), k=4):
31             pin += str(each)
32         
return pin
33
34     conn = sqlite3.connect(
"card.s3db")
35     curr = conn.cursor()
36     iin = [
4, 0, 0, 0, 0, 0]
37     random_acc_no = random.sample(range(
9), 9)
38     luhn_card_no = []
39     luhn_card_no.extend(iin)
40     luhn_card_no.extend(random_acc_no)
41     tmp_list = luhn_card_no.copy()
42     
for i in range(0, len(tmp_list), 2):
43         tmp_list[i] *=
2
44         
if tmp_list[i] > 9:
45             tmp_list[i] -=
9
46     checksum = list(str(
10 - sum(tmp_list) % 10))
47     
if len(checksum) != 1:
48         checksum = [
0]
49     luhn_card_no.extend(checksum)
50     del tmp_list
51     card_no_for_db =
''.join(map(str, luhn_card_no))
52     card_pin_for_db = get_pin()
53     print(
"\nYour card has been created")
54     print(
"Your card number:\n{}\nYour card PIN:\n{}\n".format(card_no_for_db, card_pin_for_db))
55     curr.execute(
'SELECT id from card;')
56     db_return = curr.fetchall()
57     
try:
58         listofrows = (lambda l: [item
for sublist in l for item in sublist])(db_return)
59         myid = max(listofrows)
60     except ValueError:
61         myid =
0
62     dontsqlinjectme = (myid, card_no_for_db, card_pin_for_db)
63     curr.execute(
'INSERT INTO card (id, number, pin) VALUES (?, ?, ?);', dontsqlinjectme)
64     conn.commit()
65
66
67 def retrieve_from_db(user_enters_card_no, user_enters_pin):
68     conn = sqlite3.connect(
"card.s3db")
69     curr = conn.cursor()
70     card_number = user_enters_card_no
71     pin = user_enters_pin
72     dontsqlinjectme = (card_number, pin)
73     curr.execute(
'SELECT number, pin FROM card WHERE number = ? and pin = ?;', dontsqlinjectme)
74     db_return = curr.fetchone()
75     match = False
76     
try:
77         
if card_number in db_return and pin in db_return:
78             match = True
79             print(
"You have successfully logged in!")
80     except sqlite3.OperationalError:
81         print(
"\nWrong card number or PIN!\n")
82     except TypeError:
83         print(
"\nWrong card number or PIN!\n")
84     
while match:
85         print(
"User Card Management System\n1. Balance\n2. Add Money\n3. Transfer money\n4. Close account\n5.Log out\n0.Exit")
86         second_menu_choice =
int(input())
87         
if second_menu_choice == 1:
88             curr.execute(
'SELECT balance FROM card WHERE number = ? and pin = ?;', (card_number, pin))
89             db_return = curr.fetchone()
90             print(
"\nBalance: {}\n".format(db_return[0]))
91         elif second_menu_choice ==
2:
92             print(
"\nEnter Money:")
93             dontsqlinjectme = (
int(input()), card_number, pin)
94             curr.execute(
'UPDATE card SET balance = balance + ? WHERE number = ? and pin = ?;', dontsqlinjectme)
95             conn.commit()
96             print(
"Money was added!")
97         elif second_menu_choice ==
3:
98             
global transfer_destination
99             transfer_destination = []
100             print(
"Enter card number:")
101             user_enters_transferdest = input()
102             
if len(user_enters_transferdest) != 16:
103                 print(
"\nProbably you made a mistake in the card number.\nPlease try again!\n")
104                 
continue
105             elif len(user_enters_transferdest) ==
16:
106                 
if user_enters_transferdest == card_number:
107                     print(
"\nYou can't transfer money to the same account!\n")
108                     
continue
109                 elif not check_luhn_algo(user_enters_transferdest):
110                     
'# IF CHECK LUHN ALGO RETURNS FALSE. NOT FALSE = TRUE AND THEN WE CONTINUE'
111                     print(
"\nLUHN CHECK:Probably you made a mistake in the card number.\nPlease try again!\n")
112                     
continue
113                 
else:
114                     transfer_destination = (
int(user_enters_transferdest),)
115             curr.execute(
'SELECT number FROM card WHERE number = ?;', transfer_destination)
116             db_return = curr.fetchone()
117             
try:
118                 len(db_return)
119                 print(
"\nEnter how much money you want to transfer:\n")
120                 user_enters_transfermoney =
int(input())
121                 curr.execute(
'SELECT balance FROM card WHERE number = ? and pin = ?', (card_number, pin))
122                 db_return = curr.fetchone()
123                 
if user_enters_transfermoney > db_return[0]:
124                     print(
"\nNot enough money!\n")
125                     
continue
126                 
else:
127                     curr.execute(
'UPDATE card SET balance = balance + ? WHERE number = ?;', (
128                         user_enters_transfermoney,
int(user_enters_transferdest)))
129                     curr.execute(
'UPDATE card SET balance = balance - ? WHERE number = ?;', (
130                         user_enters_transfermoney, card_number))
131                     conn.commit()
132                     print(
"\nSuccess!\n")
133                     
continue
134             except TypeError:
135                 print(
"\nSuch a card does not exist.\n")
136                 
continue
137
138         elif second_menu_choice ==
4:
139             dontsqlinjectme = (card_number, pin)
140             curr.execute(
'DELETE FROM card WHERE number = ? and pin = ?;', dontsqlinjectme)
141             conn.commit()
142             print(
"\nThe account has been closed!\n")
143             
break
144         elif second_menu_choice ==
5:
145             print(
"You have successfully logged out!")
146             match = False
147         elif second_menu_choice ==
0:
148             print(
"Bye!")
149             conn.close()
150             exit()
151
152
153 def create_db():
154     conn = sqlite3.connect(
"card.s3db")
155     curr = conn.cursor()
156     
try:
157         curr.execute(
'create table card (id INTEGER, number TEXT, pin TEXT, balance INTEGER default 0);')
158     except sqlite3.OperationalError:
159         curr.execute(
'DROP TABLE card;')
160         curr.execute(
'create table card (id INTEGER, number TEXT, pin TEXT, balance INTEGER default 0);')
161     
finally:
162         conn.commit()
163
164
165 program_is_running = True
166 create_db()

167 while
program_is_running:
168     print(
"====Welcome to User Card Management System====\n1. Create New Account(Auto Creation)\n2. Log Account\n0. Exit")
169     first_menu_choice =
int(input())
170     
if first_menu_choice == 1:
171         create_card()
172     elif first_menu_choice ==
2:
173         print(
"Enter your card number:")
174         user_enters_card_no = input()
175         print(
"Enter your PIN:")
176         user_enters_pin = input()
177         retrieve_from_db(user_enters_card_no, user_enters_pin)
178     elif first_menu_choice ==
0:
179         print(
"Thank you! Bye!")
180         program_is_running = False


Gõ tìm kiếm nhanh...